//
//  MeUserCell.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/7.
//

import UIKit

class MeUserCell: UITableViewCell {
    
    lazy var iconImage: UIImageView = {
        return UIImageView()
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 16)
        label.textColor = .black
        return label
    }()
    
    lazy var arrowImage: UIImageView = {
        return UIImageView(image: UIImage(named: "setting_right_arrow"))
    }()
    
    lazy var rightLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textColor = .gray
        label.textAlignment = .right
        label.isHidden = true
        return label
    }()
    
    lazy var pointView: UIView = {
        let view = UIView()
        view.backgroundColor = .init(named: "red_FE6390")
        view.wn_cornerWithBorder(4)
        view.isHidden = true
        return view
    }()
    
    lazy var subImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "setting_sub_image"))
        image.wn_cornerWithBorder(4)
        image.isHidden = true
        return image
    }()
    
    lazy var inviteImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "invite_reward_bg"))
        image.wn_cornerWithBorder(4)
        image.isHidden = true
        return image
    }()
    
    lazy var topupTaskView: TopupTaskView = {
        let view = TopupTaskView()
        view.isHidden = true
        return view
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .white
        contentView.addSubview(iconImage)
        contentView.addSubview(titleLabel)
        contentView.addSubview(arrowImage)
        contentView.addSubview(rightLabel)
        contentView.addSubview(pointView)
        contentView.addSubview(subImage)
        contentView.addSubview(inviteImage)
        contentView.addSubview(topupTaskView)
        iconImage.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(12)
            make.width.height.equalTo(24)
        }
        titleLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(iconImage.snp.right).offset(10)
        }
        arrowImage.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(-12)
            make.width.height.equalTo(24)
        }
        rightLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(arrowImage.snp.left)
        }
        pointView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(rightLabel.snp.right)
            make.width.height.equalTo(8)
        }
        subImage.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(20)
            make.bottom.equalTo(-12)
            make.right.equalTo(-20)
        }
        inviteImage.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        topupTaskView.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(20)
            make.bottom.equalTo(-12)
            make.right.equalTo(-20)
        }
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapType))
        self.addGestureRecognizer(tap)
    }
    
    var userType: Int? {
        didSet {
            iconImage.image = UIImage(named: "")
            titleLabel.text = ""
            rightLabel.isHidden = true
            pointView.isHidden = true
            subImage.isHidden = true
            inviteImage.isHidden = true
            topupTaskView.isHidden = true
            arrowImage.isHidden = false
            switch userType {
            case 0:
                subImage.isHidden = true
                arrowImage.isHidden = true
            case 1:
                topupTaskView.isHidden = false
                arrowImage.isHidden = true
            case 2:
                iconImage.image = UIImage(named: "me_earnrewards")
                titleLabel.text = "賺取獎勵"
                pointView.isHidden = getHasCheck()
            case 3:
                iconImage.image = UIImage(named: "me_task")
                titleLabel.text = "任務"
            case 4:
                iconImage.image = UIImage(named: "me_history")
                titleLabel.text = "閱讀歷史"
            case 10:
                inviteImage.isHidden = false
            case 20:
                iconImage.image = UIImage(named: "me_inbox")
                titleLabel.text = "收件匣"
            case 21:
                iconImage.image = UIImage(named: "me_preference")
                titleLabel.text = "偏好設定"
            case 22:
                iconImage.image = UIImage(named: "me_setting")
                titleLabel.text = "設定"
            default: break
            }
        }
    }
    
    @objc func tapType() {
        switch userType {
        case 0:
            let vc = WNSubVC()
            vc.hidesBottomBarWhenPushed = true
            viewContainingController?.navigationController?.pushViewController(vc, animated: true)
        case 1:
            break
        case 2:
            viewContainingController?.wn_pushEarnVC()
        case 3:
            viewContainingController?.wn_pushTaskVC()
        case 4:
            let vc = WNViewedVC()
            vc.hidesBottomBarWhenPushed = true
            viewContainingController?.navigationController?.pushViewController(vc, animated: true)
        case 10:
            let vc = WNInviteVC()
            vc.hidesBottomBarWhenPushed = true
            viewContainingController?.navigationController?.pushViewController(vc, animated: true)
        case 20:
            let vc = WNInboxVC()
            vc.hidesBottomBarWhenPushed = true
            viewContainingController?.navigationController?.pushViewController(vc, animated: true)
        case 21:
            let vc = WNPreferenceVC()
            vc.hidesBottomBarWhenPushed = true
            viewContainingController?.navigationController?.pushViewController(vc, animated: true)
        case 22:
            let vc = WNSettingVC()
            vc.hidesBottomBarWhenPushed = true
            viewContainingController?.navigationController?.pushViewController(vc, animated: true)
        default: break
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class TopupTaskView: UIView {
    
    lazy var topupImage: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setBackgroundImage(UIImage(named: "setting_topup_task"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            self?.topupClosure?(self?.model)
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(4)
        return btn
    }()

    lazy var topupLabel: UILabel = {
        let label = UILabel()
        label.text = "限時儲值"
        label.textColor = .init(named: "black_222222")
        label.font = .wn_font(name: .bold, size: 16)
        return label
    }()
        
    lazy var priceLabel: UILabel = {
        let label = UILabel()
        label.textColor = .init(named: "black_333333")
        label.font = .wn_font(name: .bold, size: 16)
        return label
    }()
        
    lazy var sunBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setBackgroundImage(UIImage(named: "setting_topup_sun"), for: .normal)
        btn.setTitleColor(.init(named: "red_FE6390"), for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 12)
        btn.titleLabel?.textAlignment = .center
        return btn
    }()

    lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = .init(named: "purple_AB95FE")
        label.textColor = .white
        label.font = .wn_font(name: .bold, size: 12)
        label.textAlignment = .center
        label.wn_cornerWithBorder(4)
        return label
    }()
        
    var expireTimer: Timer?

    var expireTime = 0
    
    var topupClosure: AnyClosure<RechargeDetailModel?>?
    
    var expireClosure: ReturnClosure?
    
    var model: RechargeDetailModel? {
        didSet {
            if let m = model {
                sunBtn.setTitle("+\(m.disGold)%", for: .normal)
                let str = "USD \(m.price) 得到 \(m.gold) + \(m.goldGive) 金幣"
                priceLabel.attributedText = str.wn_numberChange(.init(named: "red_FE6390")!, .wn_font(name: .bold, size: 18))
                startExpireTimer()
            }
        }
    }
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(topupImage)
        topupImage.addSubview(topupLabel)
        topupImage.addSubview(priceLabel)
        topupImage.addSubview(sunBtn)
        topupImage.addSubview(timeLabel)
        topupImage.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        topupLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(12)
        }
        priceLabel.snp.makeConstraints { (make) in
            make.top.equalTo(topupLabel.snp.bottom).offset(4)
            make.centerX.equalToSuperview()
        }
        sunBtn.snp.makeConstraints { make in
            make.top.equalTo(6)
            make.right.equalTo(-6)
            make.size.equalTo(CGSize.init(width: 52, height: 52))
        }
        timeLabel.snp.makeConstraints { (make) in
            make.top.equalTo(6)
            make.left.equalTo(6)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func startExpireTimer() {
        stopExpireTimer()
        expireTime = model?.expireTime ?? 0
        if expireTime != 0 {
            expireTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(countdown), userInfo: nil, repeats: true)
            RunLoop.current.add(expireTimer!, forMode: .common)
        }
    }
    
    @objc func countdown() {
        expireTime -= 1
        if expireTime <= 0 {
            stopExpireTimer()
            timeLabel.text = ""
            expireClosure?()
        }else {
            timeLabel.text = "\(expireTime)".wn_toDuration() + " "
        }
    }
    
    func stopExpireTimer() {
        if expireTimer != nil {
            expireTimer?.invalidate()
            expireTimer = nil
        }
        expireTime = 0
    }
}
